package com.ls.ai.controller;

import com.ls.ai.advisor.PrintLoggerAdvisor;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/client")
public class DashScopeChatClientController {

	private final ChatClient deepClient;

	public DashScopeChatClientController(ChatClient.Builder chatClientBuilder) {
		this.deepClient = chatClientBuilder.defaultAdvisors(new PrintLoggerAdvisor()).build();
	}

	/**
	 * ChatClient 简单调用
	 */
	@GetMapping("/simple/chat")
	public String simpleChat(@RequestParam("prompt") String prompt) {

		return deepClient.prompt(prompt).call().content();
	}

	/**
	 * ChatClient 流式调用
	 */
	@GetMapping("/stream/chat")
	public Flux<String> streamChat(HttpServletResponse response,@RequestParam("prompt") String prompt) {

		response.setCharacterEncoding("UTF-8");
		return deepClient.prompt(prompt).stream().content();
	}



}
